<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>凉菜</title>

    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">

    <script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
    
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">凉菜名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="dishes_name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">存量</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="dishes_stock" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">价格</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="dishes_price" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
    
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteAll"> 批量删除 </button>
                </div>
            </script>
    
            <table class="layui-hide" id="coolTable" lay-filter="currentTableFilter"></table>
    
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>
    
        </div>
    </div>

    <script id="picTpl">
        <img src="{{d.dishes_pic}}" name="pic" style="width:100%; height:100%;"/>
    </script>

    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;
    
            table.render({
                elem: '#coolTable',
                url: 'http://localhost:8080/cool/coolList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'dishes_id', width: 80, title: 'ID', sort: true},
                    {field: 'dishes_name', width: 120, title: '特色菜名'},
                    {field: 'dishes_stock', width: 120, title: '存量', sort: true},
                    {field: 'dishes_price', width: 150, title: '价格'},              
                    {field: 'dishes_pic', width: 150, title: '图片' ,templet: '#picTpl'},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 5,
                page: true,
                skin: 'line'
            });
    
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });
    
                //执行搜索重载
                table.reload('coolTable', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result
                        dishes_name: data.field.dishes_name,
                        dishes_stock: data.field.dishes_stock,
                        dishes_price: data.field.dishes_price,
                    }
                }, 'data');
    
                return false;
            });
    
            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加学员',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: 'cooladd.html',
                        end:function(index){
                            table.reload('coolTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('coolTable')
                        , data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }else if(obj.event === 'deleteAll'){//监听批量删除操作
                    //deleteAll指的是批量删除按钮的event值
                    var checkStatus = table.checkStatus('coolTable')
                    , data = checkStatus.data;
                    //创建一个数组selectids去获取 批量删除复选框的id
                    //存到数组中
                    var selectids = new Array();
                    //计数器
                    var count  = 0;
                    $.each(data,function(index,value){
                        //遍历数组，将复选框 checkStatus.data 的值传入selectids数组中
                        //传过来一个id，则count+1
                        selectids [count] = value.dishes_id;
                        count++;
                    })
                    console.log(ids);
                    //layer.alert(JSON.stringify(data));
                    var ids = encodeURI(selectids);
                    if(selectids.length != 0){
                        $.post("http://localhost:8080/cool/deleteAllCool",{dishes_id:ids},
                        function(result){
                            if(result.code == 20000){
                                layer.msg("批量删除成功",{icon:1,time:2000},
                                function(){
                                    setTimeout("layui.table.reload('coolTable')",200);
                                }
                                
                                );
                            }else if(result.code == 4444){
                                layer.alert("批量删除失败",{icon:5,time:2000});
                            }
                        },"json");
                    }else{
                        layer.alert("没有选中任何商品信息!",{icon:2,time:2000})
                    }
                        layer.alert(JSON.stringify(data));
                }

            }); 


            // //监听表格复选框选择
            // table.on('checkbox(currentTableFilter)', function (obj) {
            //     console.log(obj)
            // });
    
            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    
                   //把要修改的数据存到 localStorage        跳转到修改页面之后  再取出来 
                    localStorage.setItem("filter",JSON.stringify(data));
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: 'cooledit.html',
                        end:function(index){
                            // 列表重新加载
                            table.reload('coolTable');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        $.get("http://localhost:8080/cool/deleteCool",{dishes_id:data.dishes_id},function(result){
                            if(result.code==20000){
                                layer.msg("删除成功");
                                obj.del();   //删除对应的  tr  删除页面上的dom 结构
                            }    
                            
                        },"json");    

                        layer.close(index);
                    });
                }
            });
            });
    
            
    </script>
</body>
</html>